home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / zfilterx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-03  |  9.0 KB  |  315 lines

  1. /* Copyright (C) 1995, 1996 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zfilterx.c */
  20. /* Extended (non-standard) filter creation */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "gsstruct.h"
  26. #include "ialloc.h"
  27. #include "idict.h"
  28. #include "idparam.h"
  29. #include "store.h"
  30. #include "strimpl.h"
  31. #include "sfilter.h"
  32. #include "sbwbs.h"
  33. #include "sbhc.h"
  34. #include "sbtx.h"
  35. #include "shcgen.h"
  36. #include "smtf.h"
  37. #include "spcxx.h"
  38. #include "ifilter.h"
  39.  
  40. /* ------ Bounded Huffman code filters ------ */
  41.  
  42. /* Common setup for encoding and decoding filters */
  43. private int
  44. bhc_setup(os_ptr op, stream_BHC_state *pbhcs)
  45. {    int code;
  46.     int num_counts;
  47.     int data[max_hc_length + 1 + 256 + max_zero_run + 1];
  48.     uint dsize;
  49.     int i;
  50.     uint num_values, accum;
  51.     ushort *counts;
  52.     ushort *values;
  53.  
  54.     check_type(*op, t_dictionary);
  55.     check_dict_read(*op);
  56.     if ( (code = dict_bool_param(op, "FirstBitLowOrder", false,
  57.                      &pbhcs->FirstBitLowOrder)) < 0 ||
  58.          (code = dict_int_param(op, "MaxCodeLength", 1, max_hc_length,
  59.                     max_hc_length, &num_counts)) < 0 ||
  60.          (code = dict_bool_param(op, "EndOfData", true,
  61.                      &pbhcs->EndOfData)) < 0 ||
  62.          (code = dict_uint_param(op, "EncodeZeroRuns", 2, 256,
  63.                      256, &pbhcs->EncodeZeroRuns)) < 0 ||
  64.             /* Note: the code returned from the following call */
  65.             /* is actually the number of elements in the array. */
  66.          (code = dict_int_array_param(op, "Tables", countof(data),
  67.                       data)) <= 0
  68.        )
  69.       return (code < 0 ? code : gs_note_error(e_rangecheck));
  70.     dsize = code;
  71.     if ( dsize <= num_counts + 2 )
  72.       return_error(e_rangecheck);
  73.     for ( i = 0, num_values = 0, accum = 0; i <= num_counts;
  74.           i++, accum <<= 1
  75.         )
  76.       {    int count = data[i];
  77.         if ( count < 0 )
  78.           return_error(e_rangecheck);
  79.         num_values += count;
  80.         accum += count;
  81.       }
  82.     if ( dsize != num_counts + 1 + num_values ||
  83.          accum != 1 << (num_counts + 1) ||
  84.          pbhcs->EncodeZeroRuns >
  85.            (pbhcs->EndOfData ? num_values - 1 : num_values)
  86.        )
  87.       return_error(e_rangecheck);
  88.     for ( ; i < num_counts + 1 + num_values; i++ )
  89.       {    int value = data[i];
  90.         if ( value < 0 || value >= num_values )
  91.           return_error(e_rangecheck);
  92.       }
  93.     pbhcs->definition.counts = counts =
  94.       (ushort *)ialloc_byte_array(num_counts + 1, sizeof(ushort),
  95.                       "bhc_setup(counts)");
  96.     pbhcs->definition.values = values =
  97.       (ushort *)ialloc_byte_array(num_values, sizeof(ushort),
  98.                       "bhc_setup(values)");
  99.     if ( counts == 0 || values == 0 )
  100.       {    ifree_object(values, "bhc_setup(values)");
  101.         ifree_object(counts, "bhc_setup(counts)");
  102.         return_error(e_VMerror);
  103.       }
  104.     for ( i = 0; i <= num_counts; i++ )
  105.       counts[i] = data[i];
  106.     pbhcs->definition.counts = counts;
  107.     pbhcs->definition.num_counts = num_counts;
  108.     for ( i = 0; i < num_values; i++ )
  109.       values[i] = data[i + num_counts + 1];
  110.     pbhcs->definition.values = values;
  111.     pbhcs->definition.num_values = num_values;
  112.     return 0;
  113. }
  114.  
  115. /* <target> <dict> BoundedHuffmanEncode/filter <file> */
  116. private int
  117. zBHCE(os_ptr op)
  118. {    stream_BHCE_state bhcs;
  119.     int code;
  120.     code = bhc_setup(op, (stream_BHC_state *)&bhcs);
  121.     if ( code < 0 )
  122.       return code;
  123.     return filter_write(op, 1, &s_BHCE_template, (stream_state *)&bhcs, 0);
  124. }
  125.  
  126. /* <source> <dict> BoundedHuffmanDecode/filter <file> */
  127. private int
  128. zBHCD(os_ptr op)
  129. {    stream_BHCD_state bhcs;
  130.     int code;
  131.     code = bhc_setup(op, (stream_BHC_state *)&bhcs);
  132.     if ( code < 0 )
  133.       return code;
  134.     return filter_read(op, 1, &s_BHCD_template, (stream_state *)&bhcs, 0);
  135. }
  136.  
  137. /* <array> <max_length> .computecodes <array> */
  138. /* The first max_length+1 elements of the array will be filled in with */
  139. /* the code counts; the remaining elements will be replaced with */
  140. /* the code values.  This is the form needed for the Tables element of */
  141. /* the dictionary parameter for the BoundedHuffman filters. */
  142. private int
  143. zcomputecodes(os_ptr op)
  144. {    os_ptr op1 = op - 1;
  145.     uint asize;
  146.     hc_definition def;
  147.     ushort *data;
  148.     long *freqs;
  149.     int code = 0;
  150.     check_type(*op, t_integer);
  151.     check_write_type(*op1, t_array);
  152.     asize = r_size(op1);
  153.     if ( op->value.intval < 1 || op->value.intval > max_hc_length )
  154.       return_error(e_rangecheck);
  155.     def.num_counts = op->value.intval;
  156.     if ( asize < def.num_counts + 2 )
  157.       return_error(e_rangecheck);
  158.     def.num_values = asize - (def.num_counts + 1);
  159.     data = (ushort *)gs_alloc_byte_array(imemory, asize, sizeof(ushort),
  160.                          "zcomputecodes");
  161.     freqs = (long *)gs_alloc_byte_array(imemory, def.num_values,
  162.                         sizeof(long),
  163.                         "zcomputecodes(freqs)");
  164.     if ( data == 0 || freqs == 0 )
  165.       code = gs_note_error(e_VMerror);
  166.     else
  167.       {    uint i;
  168.         def.counts = data;
  169.         def.values = data + (def.num_counts + 1);
  170.         for ( i = 0; i < def.num_values; i++ )
  171.           {    const ref *pf =
  172.               op1->value.const_refs + i + def.num_counts + 1;
  173.             if ( !r_has_type(pf, t_integer) )
  174.               {    code = gs_note_error(e_typecheck);
  175.                 break;
  176.               }
  177.             freqs[i] = pf->value.intval;
  178.           }
  179.         if ( !code )
  180.           {    code = hc_compute(&def, freqs, imemory);
  181.             if ( code >= 0 )
  182.               {    /* Copy back results. */
  183.                 for ( i = 0; i < asize; i++ )
  184.                   make_int(op1->value.refs + i, data[i]);
  185.               }
  186.           }
  187.       }
  188.     gs_free_object(imemory, freqs, "zcomputecodes(freqs)");
  189.     gs_free_object(imemory, data, "zcomputecodes");
  190.     if ( code < 0 )
  191.       return code;
  192.     pop(1);
  193.     return code;
  194. }
  195.  
  196. /* ------ Burrows/Wheeler block sorting filters ------ */
  197.  
  198. /* Common setup for encoding and decoding filters */
  199. private int
  200. bwbs_setup(os_ptr op, stream_BWBS_state *pbwbss)
  201. {    int code;
  202.     if ( (code = dict_int_param(op, "BlockSize",
  203.                     1, max_int / sizeof(int) - 10, 16384,
  204.                     &pbwbss->BlockSize)) < 0
  205.        )
  206.         return code;
  207.     return 0;
  208. }
  209.  
  210. /* <target> <dict> BWBlockSortEncode/filter <file> */
  211. private int
  212. zBWBSE(os_ptr op)
  213. {    stream_BWBSE_state bwbss;
  214.     int code;
  215.     check_type(*op, t_dictionary);
  216.     check_dict_read(*op);
  217.     code = bwbs_setup(op, (stream_BWBS_state *)&bwbss);
  218.     if ( code < 0 )
  219.       return code;
  220.     return filter_write(op, 1, &s_BWBSE_template, (stream_state *)&bwbss, 0);
  221. }
  222.  
  223. /* <source> <dict> BWBlockSortDecode/filter <file> */
  224. private int
  225. zBWBSD(os_ptr op)
  226. {    stream_BWBSD_state bwbss;
  227.     int code;
  228.     code = bwbs_setup(op, (stream_BWBS_state *)&bwbss);
  229.     if ( code < 0 )
  230.       return code;
  231.     return filter_read(op, 1, &s_BWBSD_template, (stream_state *)&bwbss, 0);
  232. }
  233.  
  234. /* ------ Byte translation filters ------ */
  235.  
  236. /* Common setup */
  237. private int
  238. bt_setup(os_ptr op, stream_BT_state *pbts)
  239. {    int npop = 1;
  240.     if ( r_has_type(op, t_dictionary) )
  241.       ++npop, --op;
  242.     check_read_type(*op, t_string);
  243.     if ( r_size(op) != 256 )
  244.       return_error(e_rangecheck);
  245.     memcpy(pbts->table, op->value.const_bytes, 256);
  246.     return npop;
  247. }
  248.  
  249. /* <target> <table> ByteTranslateEncode/filter <file> */
  250. /* <target> <table> <dict_ignored> ByteTranslateEncode/filter <file> */
  251. private int
  252. zBTE(os_ptr op)
  253. {    stream_BT_state bts;
  254.     int code = bt_setup(op, &bts);
  255.  
  256.     if ( code < 0 )
  257.       return code;
  258.     return filter_write(op, code, &s_BTE_template, (stream_state *)&bts,
  259.                 0);
  260. }
  261.  
  262. /* <target> <table> ByteTranslateDecode/filter <file> */
  263. /* <target> <table> <dict_ignored> ByteTranslateDecode/filter <file> */
  264. private int
  265. zBTD(os_ptr op)
  266. {    stream_BT_state bts;
  267.     int code = bt_setup(op, &bts);
  268.  
  269.     if ( code < 0 )
  270.       return code;
  271.     return filter_read(op, code, &s_BTD_template, (stream_state *)&bts, 0);
  272. }
  273.  
  274. /* ------ Move-to-front filters ------ */
  275.  
  276. /* <target> MoveToFrontEncode/filter <file> */
  277. /* <target> <dict_ignored> MoveToFrontEncode/filter <file> */
  278. private int
  279. zMTFE(os_ptr op)
  280. {    return filter_write_simple(op, &s_MTFE_template);
  281. }
  282.  
  283. /* <source> MoveToFrontDecode/filter <file> */
  284. /* <source> <dict_ignored> MoveToFrontDecode/filter <file> */
  285. private int
  286. zMTFD(os_ptr op)
  287. {    return filter_read_simple(op, &s_MTFD_template);
  288. }
  289.  
  290. /* ------ PCX decoding filter ------ */
  291.  
  292. /* <source> PCXDecode/filter <file> */
  293. /* <source> <dict_ignored> PCXDecode/filter <file> */
  294. private int
  295. zPCXD(os_ptr op)
  296. {    return filter_read_simple(op, &s_PCXD_template);
  297. }
  298.  
  299. /* ================ Initialization procedure ================ */
  300.  
  301. BEGIN_OP_DEFS(zfilterx_op_defs) {
  302.     {"2.computecodes", zcomputecodes},    /* not a filter */
  303.         op_def_begin_filter(),
  304.         /* Non-standard filters */
  305.     {"2BoundedHuffmanEncode", zBHCE},
  306.     {"2BoundedHuffmanDecode", zBHCD},
  307.     {"2BWBlockSortEncode", zBWBSE},
  308.     {"2BWBlockSortDecode", zBWBSD},
  309.     {"2ByteTranslateEncode", zBTE},
  310.     {"2ByteTranslateDecode", zBTD},
  311.     {"1MoveToFrontEncode", zMTFE},
  312.     {"1MoveToFrontDecode", zMTFD},
  313.     {"1PCXDecode", zPCXD},
  314. END_OP_DEFS(0) }
  315.